require 'login_system'
class Plugin::NabaztagController < ActionController::Base
  before_filter :set_site_template_root
  def set_site_template_root
    self.class.template_root = "#{RAILS_ROOT}/components/"
    @template.base_path = template_root
  end 
  include LoginSystem
  before_filter :login_required, :only => [:config]
  
verify :method => :post, :only => [ :send_message ],
         :redirect_to => { :action => :show }  
  
  
  def show
    create_oper
    @plugin=Extplugin.find_by_name("nabaztag")
	  @config=@plugin.config 
	if @plugin.activated==1
  	  render(:layout => false)
  	else
  	  render :text => _("not activated")
  	end   	
  end
	
  def config
    @plugin=Extplugin.find_by_name("nabaztag")
    @config=@plugin.config 
  end

  def send_message
  	if nabcheck
      @plugin=Extplugin.find_by_name("nabaztag")
      @config=@plugin.config 
      h = Net::HTTP.new('api.nabaztag.com', 80)
      @resp, @data = h.get('/vl/FR/api.jsp?sn='+@config["serial"].to_s+'&token='+@config["token"].to_s+'&tts='+CGI::escape(params[:message])+'&voice='+params[:voice].to_s+'&posleft='+params[:left].to_i.to_s+'&posright='+params[:right].to_i.to_s+'&speed='+params[:speed].to_s+'&pitch='+params[:pitch].to_s, nil )
    end
    create_oper
  end

protected

  def create_oper
    session["oper1"]=0
    session["oper2"]=0
    while (eval(session["oper1"].to_s+session["sign"].to_s+session["oper2"].to_s) == 0)
      session["oper1"]=rand(10)
      session["oper2"]=rand(10)
      @oper=["+","-","*"]
      session["sign"]=@oper[rand(3)] 
    end
  end
  
  def nabcheck
    eval(session["oper1"].to_s+session["sign"].to_s+session["oper2"].to_s)==@params[:validation].to_s.to_i
  end
   
end